package ar.com.cooker.services.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.cooker.services.RecipeServices;
import ar.com.cooker.services.pojo.RecipePojo;
import ar.com.cooker.services.uow.recipe.GetAllRecipeByNameUOW;
import ar.com.cooker.services.uow.recipe.GetAllRecipeUOW;
import ar.com.cooker.services.uow.recipe.GetRecipeByIdUOW;
import ar.com.cooker.services.uow.recipe.SaveAndUpdateRecipeOUW;
import ar.com.cooker.services.util.ManagerBean;

@Path("/recipeServices")
@Component(value = "recipeServiceBean")
public class RecipeServicesImpl implements RecipeServices {

	@Autowired
	private GetAllRecipeUOW getAllRecipeUOW;
	
	@Autowired
	private GetAllRecipeByNameUOW getAllRecipeByNameUOW;
	
	@Autowired
	private SaveAndUpdateRecipeOUW saveAndUpdateRecipeOUW;
	
	@Autowired
	private GetRecipeByIdUOW getRecipeByIdUOW;
	
	ManagerBean managerBean;
	
	@POST
	@Path("/post/createRecipe/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createRecipe(RecipePojo recipe) {
		if(getSaveAndUpdateRecipeOUW() == null){
			init();
		}
		getSaveAndUpdateRecipeOUW().execute(recipe);
		return Response.ok().build();
	}

	@GET
	@Path("/get/allRecipe/{callback}")
	@Produces( { MediaType.APPLICATION_JSON })
	public RecipePojo[] getAllRecipe() { 
			if(getGetAllRecipeUOW() == null)
				init();		
			RecipePojo[] recipes  = getGetAllRecipeUOW().execute();
		
		return recipes;
	}
	
	@GET
	@Path("/get/allRecipeByName/{name}/{callback}")
	@Produces( { MediaType.APPLICATION_JSON })
	public RecipePojo[] getAllRecipeByName(@PathParam("name")String name){
		if(getGetAllRecipeByNameUOW() == null)
			init();
		RecipePojo[] recipes  = getGetAllRecipeByNameUOW().execute(name);
		return recipes;
	}

	@POST
	@Path("/post/updateRecipe/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateRecipe(RecipePojo recipe) {
		if(getSaveAndUpdateRecipeOUW() == null){
			init();
		}
		getSaveAndUpdateRecipeOUW().execute(recipe);
		return Response.ok().build();
	}

	
	@DELETE
	@Path("/deleteRecipe/{idRecipe}/{callback}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response deleteRecipe(@PathParam("idRecipe")Integer idRecipe) {
		// TODO Auto-generated method stub
		return null;
	}

	@GET
	@Path("/get/allRecipeById/{id}")
	@Produces( { MediaType.APPLICATION_JSON })
	public RecipePojo getRecipeById(Integer idRecipe) {
		if(getGetRecipeByIdUOW() == null){
			init();
		}
		return getGetRecipeByIdUOW().execute(idRecipe);
	}
	
	public void setGetAllRecipeUOW(GetAllRecipeUOW getAllRecipeUOW) {
		this.getAllRecipeUOW = getAllRecipeUOW;
	}

	public GetAllRecipeUOW getGetAllRecipeUOW() {
		return getAllRecipeUOW;
	}

	public void setSaveAndUpdateRecipeOUW(SaveAndUpdateRecipeOUW saveAndUpdateRecipeOUW) {
		this.saveAndUpdateRecipeOUW = saveAndUpdateRecipeOUW;
	}

	public SaveAndUpdateRecipeOUW getSaveAndUpdateRecipeOUW() {
		return saveAndUpdateRecipeOUW;
	}
	
	private void init(){
		if(managerBean == null){			
			managerBean = ManagerBean.getManager();
			setSaveAndUpdateRecipeOUW((SaveAndUpdateRecipeOUW) managerBean.getBean("saveAndUpdateRecipeOUW"));
			setGetAllRecipeUOW((GetAllRecipeUOW) managerBean.getBean("getAllRecipeUOW"));	
			setGetAllRecipeByNameUOW((GetAllRecipeByNameUOW)managerBean.getBean("getAllRecipeByNameUOW"));
			setGetRecipeByIdUOW((GetRecipeByIdUOW) managerBean.getBean("getRecipeByIdUOW"));
		}
	}

	public void setGetAllRecipeByNameUOW(GetAllRecipeByNameUOW getAllRecipeByNameUOW) {
		this.getAllRecipeByNameUOW = getAllRecipeByNameUOW;
	}

	public GetAllRecipeByNameUOW getGetAllRecipeByNameUOW() {
		return getAllRecipeByNameUOW;
	}

	public void setGetRecipeByIdUOW(GetRecipeByIdUOW getRecipeByIdUOW) {
		this.getRecipeByIdUOW = getRecipeByIdUOW;
	}

	public GetRecipeByIdUOW getGetRecipeByIdUOW() {
		return getRecipeByIdUOW;
	}

	
}
